
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

# data sets
xy = load_iris()
print(xy)

x = xy.data
y = xy.target.reshape(-1, 1)

print(x.shape, y.shape)
data = np.c_[x, y]

x_test = np.array([6, 3, 5, 2])

k = 5

def knn(x_test, data, k):
    dist = []
    for sample in data:
        distSample = np.sqrt(np.sum(np.square(sample[:-1] - x_test)))
        dist.append([distSample, sample[-1]])
    dist.sort()
    iterm = [d[-1] for d in dist[:k]]
    result = max(iterm, key=iterm.count)
    return result

result = knn(x_test, data, k)
print("result: ", result)

# data have a look
U, S, V = np.linalg.svd(x)
P = V[:2, :].T
z = np.dot(x, P)

print("S: ", S)

z_test = np.array([-10, 2])
# z_test = np.dot(x_test, P)

plt.scatter(z[:, 0], z[:, 1], c=y)
plt.plot(z_test[0], z_test[1], 'r*')
plt.show()

print(z.shape, y.shape)
z_data = np.c_[z, y]

k=11
result1 = knn(z_test, z_data, k)
print("result1: ", result1)

